package io.renren.modules.com.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.com.entity.ComApportionEntity;
import io.renren.modules.com.entity.ComCollectionRecordEntity;
import io.renren.modules.com.service.ComApportionService;
import io.renren.modules.com.service.ComCrChargingitemService;

/**
 * 
 * 
 * @author pfp
 * @email
 * @date 2017-07-27 16:42:49
 */
@Controller
@RequestMapping("comapportion")
public class ComApportionController {

	@Autowired
	private ComApportionService comApportionService;

	@Autowired
	private ComCrChargingitemService comCrChargingitemService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("comapportion:list")
	@ResponseBody
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<Map<String, Object>> chinaJoy = comApportionService.queryList(query);
		int total = comCrChargingitemService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(chinaJoy, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{serialNo}")
	@RequiresPermissions("comapportion:info")
	@ResponseBody
	public R info(@PathVariable("serialNo") Long serialNo) {
		return comApportionService.queryObject(serialNo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("comapportion:save")
	@ResponseBody
	public R save(@RequestBody ComApportionEntity comApportion) {
		comApportionService.save(comApportion);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("comapportion:update")
	@ResponseBody
	public R update(@RequestBody ComCollectionRecordEntity comCollectionRecord) {
		comApportionService.update(comCollectionRecord);
		return R.ok();
	}

	@RequestMapping("/delete")
	@RequiresPermissions("comapportion:delete")
	@ResponseBody
	public R delete(@RequestBody Long[] serialNos) {
		comApportionService.deleteBatch(serialNos);
		return R.ok();
	}

	/**
	 * 初始化
	 */
	@RequestMapping("/initialization")
	@ResponseBody
	public R initialization() {
		return comApportionService.initialization();
	}

	/**
	 * 改变表头
	 */
	@RequestMapping("/changeJqGrid")
	@ResponseBody
	public R changeJqGrid(String apportionSd, String apportionEd) {
		return comApportionService.changeJqGrid(apportionSd, apportionEd);
	}

	@RequestMapping("/export")
	public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String("分摊明细".getBytes("utf-8"), "iso8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			comApportionService.export(out, params);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
